{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a21a3b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import CDIndexOption\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets import PricingContext\n",
    "from gs_quant.risk import CDATMSpread, CDFwdSpread, CDImpliedVolatility"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b9fa24b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f5ffbdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# View properties of a CD Index Option\n",
    "CDIndexOption.properties()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba34f6c7",
   "metadata": {},
   "source": [
    "Available properties (defaults) for determining the underlying index are:\n",
    "- index_family (iTraxx Europe): 'iTraxx Europe', 'iTraxx Europe FNSE', 'iTraxx Europe XOVER', 'CDX.NA.IG', 'CDX.NA.HY'\n",
    "- index_series (OTR): OTR\n",
    "- index_version (latest): latest\n",
    "\n",
    "Available properties for determining the option are:\n",
    "- option_type (Call): 'Call', 'Put'\n",
    "- strike (0.01): Any\n",
    "- strike_type (Spread): 'Spread', 'Price'\n",
    "- expriation_date (next market expiry - third Wednesday of month): Between 1m and 6m\n",
    "- termination_date (5y next IMM date): 5y\n",
    "- notional_amount (10000000): Any"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a073ff4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a 3 month call on an OTR iTraxx Main 5y underlier with a strike of 65\n",
    "cd_option = CDIndexOption(strike=0.0065, option_type='Call', expiration_date='19Jan22', termination_date='5y',  \n",
    "                          index_family='iTraxx Europe FNSE', notional_amount=10000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "216d883e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "with PricingContext(market_data_location='NYC'):\n",
    "    cd_option.resolve()\n",
    "cd_option.as_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76b7bb37",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(cd_option.price())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7cb184b",
   "metadata": {},
   "source": [
    "For index options we can also calculate the ATM (reference) spread, forward spread and implied annual volatility used for pricing the option\n",
    "\n",
    "- CDATMSpread: At the money value of the index in spread terms\n",
    "- CDFwdSpread: Expected forward value of the underlying index at option expiry, in spread terms\n",
    "- CDImpliedVolatility: Annual volatility for the underlying index hazard rate "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "864b04e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "cd_option.calc(CDATMSpread)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b476065f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cd_option.calc(CDFwdSpread)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4389869f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cd_option.calc(CDImpliedVolatility)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
